package yzserver.sx4a.department;

import com.sitech.portal.system.dao.ISysDeptDao;
import com.sitech.portal.system.domain.TblSYS_DEPT;
import yzserver.sx4a.RetTools;
import yzserver.sx4a.dao.IDepartmentDao;
import yzserver.sx4a.domain.DepartmentDomain;
import yzserver.sx4a.form.Department;
import yzserver.sx4a.form.Ret;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: andrew
 * Date: 14-8-15
 * Time: 上午10:14
 * To change this template use File | Settings | File Templates.
 */
public class Sx4aDepartmentImpl implements Sx4aDepartment {

    private IDepartmentDao departmentDao;
    private ISysDeptDao sysDeptDao;

    public void setDepartmentDao(IDepartmentDao departmentDao) {
        this.departmentDao = departmentDao;
    }

    public void setSysDeptDao(ISysDeptDao sysDeptDao) {
        this.sysDeptDao = sysDeptDao;
    }

    @Override
    public Department selDepartment(String departmentid) {
        DepartmentDomain domain = departmentDao.selDepartment(departmentid);
        if(domain!=null){
            Department department = new Department(domain);
            return department;
        }else{
            return null;
        }
    }

    @Override
    public Ret addDepartment(Department department) {
        try {
            departmentDao.addDepartment(new DepartmentDomain(department));
        } catch (Exception e) {
            return RetTools.getRet("创建部门失败", e);
        }
        String updatePortal = System.getProperty("updatePortal");
        if("true".equals(updatePortal)){
            //portal
            try {
                TblSYS_DEPT d = new TblSYS_DEPT();
                d.setDEPT_ID(department.getDepartmentid());
                d.setDEPT_DESC(department.getDisplayname());
                d.setDEPT_NAME(department.getDepartmentname());
                d.setSUPER_DEPT_ID(department.getDepartmentparent());
                sysDeptDao.add(d);
            } catch (Exception e) {
                return RetTools.getRet("创建部门失败", e);
            }
        }

        return RetTools.getRet("创建部门成功", null);
    }

    @Override
    public Ret updDepartment(Department department) {
        try {
            departmentDao.updDepartment(new DepartmentDomain(department));
        } catch (Exception e) {
            return RetTools.getRet("修改部门失败", e);
        }
        String updatePortal = System.getProperty("updatePortal");
        if("true".equals(updatePortal)){
            //portal
            try {
                TblSYS_DEPT d = new TblSYS_DEPT();
                d.setDEPT_ID(department.getDepartmentid());
                d.setDEPT_DESC(department.getDisplayname());
                d.setDEPT_NAME(department.getDepartmentname());
                d.setSUPER_DEPT_ID(department.getDepartmentparent());
                sysDeptDao.update(d);
            } catch (Exception e) {
                return RetTools.getRet("修改部门失败", e);
            }
        }


        return RetTools.getRet("修改部门成功", null);
    }

    @Override
    public Ret delDepartment(String departmentid) {

        try {
            departmentDao.delDepartment(departmentid);
        } catch (Exception e) {
            return RetTools.getRet("删除部门失败", e);
        }
        String updatePortal = System.getProperty("updatePortal");
        if("true".equals(updatePortal)){
            //portal
            try {
                TblSYS_DEPT tblSYS_DEPT = new TblSYS_DEPT();
                tblSYS_DEPT.setDEPT_ID(departmentid);
                sysDeptDao.delete(tblSYS_DEPT);
            } catch (Exception e) {
                return RetTools.getRet("删除部门失败", e);
            }
        }

        return RetTools.getRet("删除部门成功", null);

    }

    @Override
    public List selAllDepartment(String departmentid) {
        List<DepartmentDomain> departmentDomains = new ArrayList<DepartmentDomain>();
        List  departmentList = new ArrayList();
        if("all".equalsIgnoreCase(departmentid)){ // all为接口约定的固定值
            departmentDomains = departmentDao.selAllDepartment(departmentid);
            if(departmentDomains!=null){
                for (int i = 0; i <departmentDomains.size(); i++) {
                    Department department = new Department(departmentDomains.get(i));
                    departmentList.add(department);
                }
            }
        }
        return departmentList;
    }
}
